import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2024-11-15
 * Time: 16:54
 */
public class slousion1 {

    //折半查找
        public static int search(int[] nums, int target){
            int left=0;
            int right=nums.length-1;
            return searchchild(nums,target,left,right);
        }

        public static int searchchild(int[] nums, int target,int left,int right) {

            int mid=(left+right)/2;
            if(nums[mid]==target) return mid;
            if(left>=right) return -1;
            if(nums[mid]>target) return searchchild(nums,target,left,mid-1);
            if(nums[mid]<target) return searchchild(nums,target,mid+1,right);
            return -1;
        }

    public static void main(String[] args) {
       int[]arr=new int[]{1,2,3,4,5,6,7,8,9};

        System.out.println("数组为"+ Arrays.toString(arr));
        System.out.println("在数组中查找3，返回下标"+search(arr,3));
        System.out.println("在数组中查找10，返回下标"+search(arr,10));
    }
}

